from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
from .models import LearnUser
import sys
sys.path.append(r'..')
from django.urls import reverse
from user.models import Student,User




#由老师上传学生信息后创建的学生账号。
#账号为：手机号，密码为：身份证后6位。
def create_user(request,student_id,file_name):
    for tx_id in eval(student_id):
        s = Student.objects.get(pk = tx_id)
        cell = s.student_cell
        stu_id = s.student_id[-6:]
        s.learnuser_set.create(learnuser = cell,learnpswd = stu_id)

    return HttpResponse('创建成功,账号为手机号，密码为后六位身份证')



#学生登录界面
def login(request):
    return render(request,'learnuser/login.html')



#提交账号密码到后台
def tijiao_user(request):
    try:
        stu_user = request.POST['user']
        stu_pswd = request.POST['pswd']
        a = LearnUser.objects.get(learnuser = str(stu_user))
        stu_user_id = a.id
    except:
        return HttpResponse('账号不存在')
    else:
        if stu_pswd == a.learnpswd:
            return HttpResponseRedirect(reverse('learnuser:select', args=(stu_user_id,)))
        else:
            return HttpResponse('密码不对')

    

#选择要做试卷还是查看分数
def select(request,stu_user_id):
    context = {'stu_user_id':stu_user_id}
    return render(request,'learnuser/select.html',context)


#输入老师账号和设定的编码可以获取老师创建的试卷
def seek(request,stu_user_id):
    context = {'stu_user_id':stu_user_id}
    return render(request,'learnuser/seek.html',context)
def chuansong(request,stu_user_id):
    info = request.POST
    teacher_id = info['teacher']
    teacher_coding = info['coding']
    try:
        userid = User.objects.get(userid = teacher_id)
    except:
        return HttpResponse('未找到这个账号')
    else:
        if userid.encoding_text == teacher_coding:
            return HttpResponseRedirect(reverse('paper:paper_name',args = (teacher_id,stu_user_id,)))
        else:
            return HttpResponse('编码不对')
    #return HttpResopnseRedirect(reverse(''))




#查看老师给的分数。
def stu_score(request,stu_user_id):
    stu_obj = LearnUser.objects.get(pk = stu_user_id)
    name_obj_list = stu_obj.query_set.all()
    #paper_name = name_obj.paper_name
    #stu_score = name_obj.query_score
    context = {'stu_user_id':stu_user_id,'name_obj_list':name_obj_list}
    return render(request,'learnuser/stu_score.html',context)





























